<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<script type="text/javascript">
    var rowId = 0;

    $('fbox_branchesGrid .ui-reset , #refresh_branchesGrid').click(function(){
        $("#branchesGrid").jqGrid('setGridParam',{search:false});
        var postData = $("#branchesGrid").jqGrid('getGridParam','postData');
        $.extend(postData,{searchField:"",searchString:"",searchOper:""});
        $.extend(postData,{filters:""});
        $("#branchesGrid").trigger("reloadGrid");
    });

    $.subscribe('branchesGridRowSelected', function(event,data) {
        rowId = event.originalEvent.id;
        $( "#branchesGrid_edit" ).button( "option", "disabled", false );
        $( "#branchesGrid_delete" ).button( "option", "disabled", false );
    });

    $.subscribe('searchBranchesGrid', function(event,data) {
        $("#branchesGrid").jqGrid('searchGrid', {resize:true,drag:true,closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:"Search Branch"} );
    });

    $.subscribe('addBranchesGrid', function(event,data) {
        $("#branchesGrid").jqGrid('editGridRow',"new",{resize:true,drag:true,closeOnEscape:true,closeAfterEdit:false,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,position:"last",afterSubmit:function(response, postdata) {return isError(response.responseText);}});
    });

    $.subscribe('editBranchesGrid', function(event,data) {
        if(rowId!=0){
            $("#branchesGrid").jqGrid('editGridRow',rowId,{resize:true,drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:false,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select a branch first.");
        }
    });

    $.subscribe('deleteBranchesGrid',function(event,data){
        if(rowId!=0){
            $("#branchesGrid").jqGrid('delGridRow',rowId,{resize:true,drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,left:310,top:20,reloadAfterSubmit:false,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select a branch first.");
        }
    });
    
    $.subscribe('branchesGridComplete',function(event,data){
        $( "#branchesGrid_add" ).button( "option", "disabled", false );
        $( "#branchesGrid_search" ).button( "option", "disabled", false );
    });
    
    function isError(text) {
        if(text.indexOf('ERROR') >= 0) { return [false, text]; }
        return [true,''];
    }

</script>
<div class="gridOptsContainer">
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="branchesGrid_search" onClickTopics="searchBranchesGrid" value="Search Branch"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="branchesGrid_add" onClickTopics="addBranchesGrid" value="Add Branch"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="branchesGrid_edit" onClickTopics="editBranchesGrid" value="Edit Branch"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="branchesGrid_delete" onClickTopics="deleteBranchesGrid" value="Delete Branch"/> 
</div>
<s:url id="remoteurl" action="grid-branch"/>
<s:url id="editurl" action="edit-branch"/>
<s:url id="selectOptionsManagers" action="select-managers"/>
<sjg:grid
    id="branchesGrid"
    caption="Cashmate Branches"
    dataType="json"
    href="%{remoteurl}"
    editurl="%{editurl}"
    gridModel="gridModel"
    rowNum="-1"
    loadingText="Requesting Data..."
    width="970"
    shrinkToFit="true"
    pager="true"
    pagerButtons="false"
    pagerInput="false"
    hoverrows="true"
    rownumbers="true"
    autoencode="true"
    viewrecords="true"
    navigator="true"
    navigatorAdd="false"
    navigatorDelete="false"
    navigatorEdit="false"
    navigatorSearch="true"
    navigatorSearchOptions="{closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:'Search Branch'}"
    navigatorView="true"
    onSelectRowTopics="branchesGridRowSelected"
    onGridCompleteTopics="branchesGridComplete"
    >

    <sjg:gridColumn name="branchid"
                    index="branchid"
                    title="Branch No. (Not Editable)"
                    sortable="false"
                    key="true"
                    hidden="true"/>

    <sjg:gridColumn name="branchcode"
                    index="branchcode"
                    title="Branch Code"
                    search="true"
                    sortable="true"
                    searchoptions="{sopt:['eq']}"
                    editable="true"
                    edittype="text"
                    editoptions="{maxlength:'7'}"
                    editrules="{required:true}"
                    align="center"
                    />

    <sjg:gridColumn name="branchname"
                    index="branchname"
                    title="Branch Name"
                    sortable="true"
                    editable="true"
                    edittype="text"
                    search="true"
                    editrules="{required:true}"
                    searchoptions="{sopt:['eq']}"
                    editoptions="{maxlength:'45'}"
                    align="center"
                    />

    <sjg:gridColumn name="location"
                    index="location"
                    title="Location"
                    editoptions="{maxlength:'45'}"
                    editrules="{required:true}"
                    editable="true"
                    edittype="text"
                    align="center"
                    />

    <sjg:gridColumn name="branchmanager"
                    index="branchmanager"
                    title="Branch Manager"
                    editable="true"
                    editoptions="{dataUrl : '%{selectOptionsManagers}'}"
                    edittype="select"
                    align="center"
                    />
    
        <sjg:gridColumn name="bankaccountnum"
                    index="bankaccountnum"
                    title="Bank Account Number"
                    editable="true"
                    edittype="text"
                    align="center"
                    />
</sjg:grid>